package com.hmall.search.listrenner;

import cn.hutool.json.JSONUtil;
import com.hmall.search.dto.Item;
import com.hmall.search.mapper.SearchMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @Description SpringRabbitListener
 * @Author dmx
 * @Date 2024-11-15
 */
@Component
public class SpringRabbitListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SearchMapper searchMapper;
    @RabbitListener(queues = "simple.queue")
    public void simpleListener(String msg){
        //消息队列不为空的话重新查询，在重新插入到
        if ("hasUpdate".equals(msg)){
            //1.查询销量前20的商品
            List<Item> itemList=searchMapper.selectTopTwentieth();
            //2.将商品存储到redis中"itemList::", JSONUtil.toJsonStr(itemList)
            redisTemplate.opsForValue().set("itemList::", JSONUtil.toJsonStr(itemList));
        }
    }
}
